package com.ruoyi.module.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.page.PageFactory;
import com.ruoyi.common.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.module.app.mapper.SysUserMapper;
import com.ruoyi.module.app.domain.SysUser;
import com.ruoyi.module.app.service.ISysUserService;

import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author mars
 * @date 2023-03-10
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUser add(SysUser param) {
        super.save(param);
        return param;
    }

    @Override
    public boolean delete(Long userId) {
        return this.removeById(userId);
    }

    /**
     * 批量删除
     *
     * @author value
     * @Date 2019-02-23
     */
    @Override
    public boolean deleteBatch(List<Long> userIds) {
        return this.removeByIds(userIds);
    }

    @Override
    public boolean update(SysUser param) {
        return this.updateById(param);
    }

    @Override
    public SysUser findById(Long userId) {
        return this.getById(userId);
    }

    @Override
    @SuppressWarnings("all")
    public PageInfo findPageBySpec(SysUser param) {
        Page pageContext = getPageContext();
        LambdaQueryWrapper<SysUser> query = this.buildWrapper(param);
        log.info("查询参数：{}", JSONObject.toJSONString(query));
        IPage page = this.page(pageContext, query);
        log.info("查询返回结果：{}", JSONObject.toJSONString(page));
        return PageFactory.buildPageInfo(page);
    }

    /**
     * 构建查询条件
     *
     * @author mars
     * @Date 2023-03-10
     */
    private LambdaQueryWrapper<SysUser> buildWrapper(SysUser param) {
        LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
        if (param.getDeptId() != null){
               query.eq(SysUser::getDeptId ,param.getDeptId());
        }
         if (StringUtils.isNotBlank(param.getUserName())){
               query.like(SysUser::getUserName ,param.getUserName());
        }
         if (StringUtils.isNotBlank(param.getNickName())){
               query.like(SysUser::getNickName ,param.getNickName());
        }
        if (StringUtils.isNotBlank(param.getUserType())){
               query.eq(SysUser::getUserType ,param.getUserType());
        }
        if (StringUtils.isNotBlank(param.getEmail())){
               query.eq(SysUser::getEmail ,param.getEmail());
        }
        if (StringUtils.isNotBlank(param.getPhonenumber())){
               query.eq(SysUser::getPhonenumber ,param.getPhonenumber());
        }
        if (StringUtils.isNotBlank(param.getSex())){
               query.eq(SysUser::getSex ,param.getSex());
        }
        if (StringUtils.isNotBlank(param.getAvatar())){
               query.eq(SysUser::getAvatar ,param.getAvatar());
        }
        if (StringUtils.isNotBlank(param.getPassword())){
               query.eq(SysUser::getPassword ,param.getPassword());
        }
        if (StringUtils.isNotBlank(param.getStatus())){
               query.eq(SysUser::getStatus ,param.getStatus());
        }
        if (StringUtils.isNotBlank(param.getLoginIp())){
               query.eq(SysUser::getLoginIp ,param.getLoginIp());
        }
        if (param.getLoginDate() != null){
               query.eq(SysUser::getLoginDate ,param.getLoginDate());
        }
        return query;
    }


    private Page<Object> getPageContext() {
        return PageFactory.buildPage();
    }
}
